<?php


namespace app\common\controller;
use app\common\model\Message as MessageModel;
use think\Controller;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\Exception;
use think\exception\PDOException;

class Message extends Controller
{
    // 1 审核通过的留言 0 审核未通过的留言
    public function selectAllMessage($show){
        $message = new MessageModel();
        try {
            return  $data = $message
                ->where("msg_show", $show)
                ->where("msg_delete_time",null)
                ->order("msg_create_time desc")
                ->all();
        } catch (DataNotFoundException $e) {
        }
    }


/*=================================插入=============================================*/
    public function insertMessage($msgAuthor, $msgContent, $msgAuthorEmail){
        $data = [
            'msg_author'            =>  $msgAuthor,
            'msg_content'           =>  "$msgContent",
            'msg_author_email'      =>  "$msgAuthorEmail",
            'msg_create_time'       =>  date('Y-m-d H:i:s'),
            'msg_show'              =>  0,
        ];
        return Db::table('b_message')->insert($data,true);
    }

    /*==================================== 更新 ==================================================*/
    public function updateMessageById($msgId){
        try {
            return Db::table('b_message')
                ->where("msg_id", $msgId)
                ->update(['msg_show' => 1]);
        } catch (PDOException $e) {
        } catch (Exception $e) {
        }
    }

    /*===================================  删除=======================================================*/
    public function softDelMsgById($msgId){
        try {
            return Db::table('b_message')
                ->where('msg_id', $msgId)
                ->useSoftDelete('msg_delete_time', date('Y-m-d H:i:s'))
                ->delete();
        } catch (PDOException $e) {
        } catch (Exception $e) {
        }
    }
}